//
//  BrokerRegisterVC.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/6/9.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "BrokerRegisterVC.h"
#import "UIView+circular.h"
#import "SelecteStoreVc.h"

#import "StringTool.h"
#import "LoginDataMange.h"
#import "MBProgressHUD+MJ.h"
#import "BrokerRegistModel.h"
#import "config.h"

@interface BrokerRegisterVC ()
@property (weak, nonatomic) IBOutlet UITextField *name;

//常用手机号
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
//验证码
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
//创建密码
@property (weak, nonatomic) IBOutlet UITextField *setupPwdTextField;
//确认密码
@property (weak, nonatomic) IBOutlet UITextField *confirmTextField;
//识别码
@property (weak, nonatomic) IBOutlet UITextField *shiBieCodeTextFiled;
//所属门店
@property (weak, nonatomic) IBOutlet UITextField *storeTextField;
//获取验证码的按钮
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;

@property (weak, nonatomic) IBOutlet UIView *view1;
@property (weak, nonatomic) IBOutlet UIView *view2;
@property (weak, nonatomic) IBOutlet UIView *view3;
@property (weak, nonatomic) IBOutlet UIView *view4;
@property (weak, nonatomic) IBOutlet UIView *view5;
@property (weak, nonatomic) IBOutlet UIView *view6;
@property (nonatomic,strong)BrokerRegistModel *model;

@property (nonatomic,copy)NSString *storeCode;
@property (nonatomic,strong)LoginDataMange *dataMange;
@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;

@property (nonatomic,strong) NSTimer *timer;

@property (nonatomic,assign)NSInteger timeout;
@property (nonatomic,assign)BOOL btnEnable;
@property (weak, nonatomic) IBOutlet UIView *view7;


@end

@implementation BrokerRegisterVC
@synthesize timer;

-(void)addTimer;
{
    self.timeout = 60;
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(Countdown) userInfo:nil repeats:YES];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}


-(void)initView
{
    [self.titleView setMianTitle:@"经纪人注册"];
    self.titleView.doneBtn.hidden = YES;
    self.navigationController.navigationBarHidden = NO;
    
    
    
    [self.view1 viewToCircular];
    [self.view2 viewToCircular];
    [self.view3 viewToCircular];
    [self.view4 viewToCircular];
    [self.view5 viewToCircular];
    [self.view6 viewToCircular];
    [self.codeBtn viewToCircular];
    [self.view7 viewToCircular];
    [self.saveBtn viewToCircular];
    
    self.dataMange = [[LoginDataMange alloc]init];
}

- (IBAction)btnOnClick:(UIButton *)sender {
    
    if (sender.tag == 0) {//验证码
        
        if ([self checkPhone]) {
            [self getCode];
        }
        
    }else if (sender.tag == 1){//选择门店
        
        if ([StringTool isBlankString:self.shiBieCodeTextFiled.text]) {
            [MBProgressHUD showError:@"请输入经销商提供的识别码"];
            return;
        }
        [self selectStore];
    }else if (sender.tag == 2)//保存数据
    {
        if ([self checkInof]) {
            [self updata];
        }
    }
    
}

#pragma mark 选择门店
-(void)selectStore
{
    __weak typeof(self)bself = self;
    SelecteStoreVc *vc = [[SelecteStoreVc alloc]initWithBlock:^(NearbyCellData *data) {
        
        bself.storeTextField.text = data.title;
        bself.storeCode = data.detail;
    }];
    vc.selectedFlag = self.storeTextField.text;
    vc.storeCode = self.shiBieCodeTextFiled.text;
    vc.vcType = BrokerAddVcType;
    vc.hidesBottomBarWhenPushed = YES;;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark 校验输入信息不为空
-(BOOL)checkInof
{
    if ([StringTool isBlankString:self.name.text]) {
        [MBProgressHUD showError:@"请输入经纪人姓名"];
        return NO;
    }else if (![self checkPhoneAndCode]) {
        return NO;
    }else if (![self checkPwd])
    {
        return NO;
    }else if ([StringTool isBlankString:self.shiBieCodeTextFiled.text])
    {
        [MBProgressHUD showError:@"请输入经销商提供的识别码"];
        return NO;
    }else if ([StringTool isBlankString:self.storeTextField.text])
    {
        [MBProgressHUD showError:@"请输入所属门店"];
        return NO;
    }
    
    self.model = [[BrokerRegistModel alloc]init];
    self.model.name = self.name.text;
    self.model.mobile = self.phoneTextField.text;
    self.model.captcha1 = self.codeTextField.text;
    self.model.password = self.setupPwdTextField.text;
    self.model.repassword = self.confirmTextField.text;
    self.model.companyCode = self.shiBieCodeTextFiled.text;
    self.model.storeCode = self.storeCode;
    
    return YES;
}

#pragma mark 提交注册数据
-(void)updata
{
    [MBProgressHUD showMessage:@"请稍等" toView:self.view];
    
    __weak typeof(self)bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod = [bself.dataMange brokerregisterWithBrokerRegistModel:bself.model];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [MBProgressHUD hideHUDForView:bself.view animated:YES];
            if (mod.resltState == ResultFail) {
                [MBProgressHUD showError:mod.resultTip];
            }else if (mod.resltState == ResultNetError)
            {
                [MBProgressHUD showError:@"网络错误"];
            }else
            {
                [bself.navigationController popViewControllerAnimated:YES];
                [MBProgressHUD showSuccess:@"注册成功"];
            }
        });
    });
}
#pragma mark 校验密码
-(BOOL)checkPwd
{
    if ([StringTool isBlankString:self.setupPwdTextField.text]) {
        [MBProgressHUD showError:@"请输入密码"];
        return NO;
    }else if ([StringTool isBlankString:self.confirmTextField.text])
    {
        [MBProgressHUD showError:@"请输入确认密码"];
        return NO;
    }else if (![self.setupPwdTextField.text isEqualToString:self.confirmTextField.text])
    {
        [MBProgressHUD showError:@"两次密码输入不一致"];
        return NO;
    }
    return YES;
}
#pragma mark 获取的验证的请求
-(void)getCode
{
    [self addTimer];
    
    __weak typeof(self)bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        ResulteMod *mod = [self.dataMange brokerregistercaptcha:self.phoneTextField.text];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (mod.resltState == ResultFail) {
                [MBProgressHUD showError:mod.resultTip];
                if ([mod.resultCode isEqualToString:@"20001"]) {
                    
                    [timer invalidate];
                    [bself.codeBtn setTitle:[NSString stringWithFormat:@"获取验证码"] forState:UIControlStateNormal];
                    bself.codeBtn.enabled = YES;
                    
                }
            }else if (mod.resltState == ResultNetError)
            {
                [MBProgressHUD showError:@"网络错误"];
            }else
            {
                [MBProgressHUD showSuccess:@"验证码已发送，注意查收"];
            }
            
        });
        
    });
    
}


#pragma mark校验手机号
-(BOOL)checkPhone
{
    if ([StringTool isBlankString:self.phoneTextField.text]) {
        [MBProgressHUD showError:@"请输入手机号"];
        return NO;
    }else if (![self validateMobile:self.phoneTextField.text])
    {
        [MBProgressHUD showError:@"请输入正确的手机号"];
        return NO;
    }
    return YES;
}
#pragma mark 校验手机号和验证码
-(BOOL)checkPhoneAndCode
{
    if ([StringTool isBlankString:self.phoneTextField.text]) {
        [MBProgressHUD showError:@"请输入手机号"];
        return NO;
    }else if (![self validateMobile:self.phoneTextField.text])
    {
        [MBProgressHUD showError:@"请输入正确的手机号"];
        return NO;
    }else if ([StringTool isBlankString:self.codeTextField.text])
    {
        [MBProgressHUD showError:@"请输入验证码"];
        return NO;
    }
    return YES;
}

- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    
    CGRect dd =  [textField.superview convertRect:textField.frame toView:self.mainView];
    
    int tvbotY = dd.size.height+dd.origin.y;
    int mid  = PMH-250-tvbotY -64;
    
    if (mid<0) {
        
        const float movementDuration = 0.3f;
        [UIView beginAnimations: nil context: nil];
        [UIView setAnimationBeginsFromCurrentState: YES];
        [UIView setAnimationDuration: movementDuration];
        
        CGRect buf = self.mainView.frame;
        buf.origin.y = up ? mid : 0;
        [self.mainView setFrame:buf];
        
        [UIView commitAnimations];
    }else if(up==false){
        
        [self viewRest];
    }
}



-(void)viewRest{
    
    
    const float movementDuration = 0.3f;
    [UIView beginAnimations: nil context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    
    CGRect buf = self.mainView.frame;
    buf.origin.y =  0;
    [self.mainView setFrame:buf];
    
    [UIView commitAnimations];
}

#pragma et delegare
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField: textField up: YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [self animateTextField:textField up:false];
    
    [textField resignFirstResponder];
    
    [self viewRest];
    return  YES;
}

#pragma mark 验证码倒计时
-(void)Countdown
{
    [self.codeBtn setTitle:[NSString stringWithFormat:@"%zd重新获取",self.timeout] forState:UIControlStateNormal];
    self.codeBtn.enabled = NO;
    self.timeout --;
    if (self.timeout < 0) {
        [timer invalidate];
        [self.codeBtn setTitle:[NSString stringWithFormat:@"获取验证码"] forState:UIControlStateNormal];
        self.codeBtn.enabled = YES;
    }
    
}


@end
